package com.spica.spicaschool.di

import android.app.Application
import androidx.room.Room
import com.spica.spicaschool.persistence.AppDatabase
import com.spica.spicaschool.persistence.dao.NewsDao
import com.spica.spicaschool.persistence.dao.SportMotionRecordDao
import com.spica.spicaschool.persistence.dao.UserDao
import com.squareup.moshi.Moshi
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton


@Module
@InstallIn(SingletonComponent::class)
object PersistenceModule {

    @Provides
    @Singleton
    fun provideMoshi(): Moshi {
        return Moshi.Builder().build()
    }

    @Provides
    @Singleton
    fun provideAppDatabase(
        application: Application,
    ): AppDatabase {
        return Room
            .databaseBuilder(application, AppDatabase::class.java, "spica_school.db")
            .fallbackToDestructiveMigration()
            .build()
    }

    @Provides
    @Singleton
    fun provideUserDao(appDatabase: AppDatabase): UserDao = appDatabase.userDao()


    @Provides
    @Singleton
    fun provideNewsDao(appDatabase: AppDatabase): NewsDao = appDatabase.newsDao()

    @Provides
    @Singleton
    fun provideSportMotionRecordDao(appDatabase: AppDatabase): SportMotionRecordDao =
        appDatabase.sportMotionRecordDao()

    // @Provides
    // @Singleton
    // fun provideDiaryDao(appDatabase: AppDatabase): DiaryDao = appDatabase.diaryDao()

}